$(function() {

    //var log = console.log;

    //for Caching
    var $content = $('#content');

    /*----------------------------------------------------------------------*/
    /* datatable plugin
    /*----------------------------------------------------------------------*/

    $content.find("table.datatable").dataTable({
        "sPaginationType": "two_button",
        "oLanguage": {
            "sLengthMenu": "Prikaži _MENU_ redova po strani",
            "sZeroRecords": "Nema podataka - !",
            "sInfo": "Prikazujem _START_ - _END_ od _TOTAL_ redova",
            "sInfoEmpty": "Prikazujem 0 do 0 od 0 redova",
            "sInfoFiltered": "(filtritam od _MAX_ ukupnih redova)"
        },
        "aoColumnDefs": [ 
        { "bSortable": false, "aTargets": [null,1,null,3,] }
        ],
        "iDisplayLength" : "25",
        "aaSorting": [[ 2, "desc" ]] 
    });

    $('.deletecaricature').live('click',function(){

        var conf = confirm("Da li ste sigurni?")
        if (!conf){
            return false;
        }

        loader();

        var id = this.id

        $.ajax({
            type: "POST",
            url: base_url+'caricature/deleteone',
            data: {
                id: id
            },
            dataType: 'json',
            success: function(data){

                if (data.action == true){

                    //log('successful action')
                    $('#'+id).parent().parent().fadeOut(900,function(){
                        $(this).remove();
                    });

                    $('.dataTables_info').fadeOut('300');

                    $.modal.close(); 

                }else if (data.action == false){

                    alert(data.msg)

                    $.modal.close();

                }else alert("Error on server:"+data.msg);


            }
        })

        return false;

    })

    /***********************************************************
    * LOADERS
    ***********************************************************/

    function loader()
    {
        $.modal(
        "<div><div class='animate'><img src='"+base_url+"assets/img/backgnds/loading.gif'></div><h2>Molim sačekajte...</h2></div>" , {
            closeHTML: "",
            containerCss:{
                height:70,
                width:120,
                borderColor:"#dedede"
            },
            opacity:20,
            overlayCss: {
                backgroundColor: "#000000"
            }
        });
    }

});